//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package event_dao

import (
	"context"
	"fmt"
	"time"

	"atomgit.com/openlinksaas-org/api-server/dao/db_ns"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/events_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type DayAddonInfoDbItem struct {
	DayAddonId string `bson:"_id"`
	ProjectId  string `bson:"projectId"`
	UserId     string `bson:"userId"`
	Day        uint32 `bson:"day"`
	Content    string `bson:"content"`
}

func (item *DayAddonInfoDbItem) ToDayAddonInfo() *events_api.DayAddonInfo {
	return &events_api.DayAddonInfo{
		ProjectId: item.ProjectId,
		UserId:    item.UserId,
		Day:       item.Day,
		Content:   item.Content,
	}
}

type _DayAddonInfoDao struct {
	mc *mongo.Collection
}

func newDayAddonInfoDao(db *mongo.Database) (*_DayAddonInfoDao, error) {
	mc := db.Collection(db_ns.DB_NS_EVENT + "day_addon")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"projectId", 1}, {"day", 1}, {"userId", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_DayAddonInfoDao{mc: mc}, nil
}

func (db *_DayAddonInfoDao) Upsert(ctx context.Context, dbItem *DayAddonInfoDbItem) error {
	dbItem.DayAddonId = db.genDayAddonId(dbItem.ProjectId, dbItem.Day, dbItem.UserId)
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": dbItem.DayAddonId, "projectId": dbItem.ProjectId},
		bson.M{"$set": dbItem}, options.Update().SetUpsert(true))
	return err
}

func (db *_DayAddonInfoDao) genDayAddonId(projectId string, day uint32, userId string) string {
	return fmt.Sprintf("%s:%d:%s", projectId, day, userId)
}

func (db *_DayAddonInfoDao) Exist(ctx context.Context, projectId string, day uint32, userId string) (bool, error) {
	dayAddonId := db.genDayAddonId(projectId, day, userId)
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": dayAddonId, "projectId": projectId})
	return count > 0, err
}

func (db *_DayAddonInfoDao) Get(ctx context.Context, projectId string, day uint32, userId string) (*DayAddonInfoDbItem, error) {
	dayAddonId := db.genDayAddonId(projectId, day, userId)
	retItem := &DayAddonInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": dayAddonId, "projectId": projectId}).Decode(retItem)
	return retItem, err
}

func (db *_DayAddonInfoDao) List(ctx context.Context, projectId string, fromTime, toTime int64, filterUserId bool, userId string) ([]*DayAddonInfoDbItem, error) {
	fromDay := db.getDay(fromTime)
	toDay := db.getDay(toTime)
	query := bson.M{"projectId": projectId, "day": bson.M{"$gte": fromDay, "$lte": toDay}}
	if filterUserId {
		query["userId"] = userId
	}
	curosr, err := db.mc.Find(ctx, query)
	if err != nil {
		return nil, err
	}
	retList := []*DayAddonInfoDbItem{}
	err = curosr.All(ctx, &retList)
	return retList, err
}

func (db *_DayAddonInfoDao) getDay(timeStamp int64) uint32 {
	t := time.Unix(timeStamp/1e3, 0)
	ret := t.Year()*10000 + int(t.Month())*100 + t.Day()
	return uint32(ret)
}
